package api

import (
	"admin100/app/dao"
	"admin100/app/model"
	"admin100/app/model/commonmodel"
	"admin100/app/service"
	"admin100/library/define"
	"admin100/library/define/defErr"
	"admin100/library/response"

	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
)

var VersionMgr = &versionMgrApi{}

type versionMgrApi struct{}

// @summary 获得版本路径列表
// @tags 设置
// @produce json
// @param   cur formData  int true "当前页数"
// @param   pNum formData  int true "每页记录数量"
// @router  /version/branch-paths [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *versionMgrApi) BranchPaths(r *ghttp.Request) {
	var req commonmodel.PageInfo
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}
	data, total := service.Version.FetchBranchPath(&req)
	response.JsonSuc(r, g.Map{"rows": data, "total": total})
}

// @summary 获得平台路径列表
// @tags 设置
// @produce json
// @param   cur formData  int true "当前页数"
// @param   pNum formData  int true "每页记录数量"
// @router  /version/platform-verpaths [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *versionMgrApi) PlatformVerpaths(r *ghttp.Request) {
	var req commonmodel.PageInfo
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}
	data, total := service.Version.FetchPlatformVerChange(&req)
	response.JsonSuc(r, g.Map{"rows": data, "total": total})
}

// @summary 获得版本操作列表
// @tags 设置
// @produce json
// @param   cur formData  int true "当前页数"
// @param   pNum formData  int true "每页记录数量"
// @router  /version/veropt-list [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *versionMgrApi) VeroptList(r *ghttp.Request) {
	var req commonmodel.PageInfo
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}
	data, total := service.Version.FetchVersionToolPath(&req)
	response.JsonSuc(r, g.Map{"rows": data, "total": total})
}

// @summary 更新路径数据
// @tags 设置
// @produce json
// @param   id formData  int false "id, 只有更新时传入"
// @param   name formData  int true "名称"
// @param   path formData  string true "路径"
// @router  /version/branchpath-edit [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *versionMgrApi) BranchpathEdit(r *ghttp.Request) {
	var req model.BranchPath
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}
	u := service.Context.GetUser(r.Context())
	req.UserId = u.Id
	service.Version.BranchPathEidt(&req)

	response.JsonSuc(r)
}

// @summary 更新平台路径数据
// @tags 设置
// @produce json
// @param   id formData  int false "id, 只有更新时传入"
// @param   name formData  int true "名称"
// @param   state formData  int true "启用状态"
// @param   type formData  string true "操作类型1:打包 2:同步 3更新"
// @param   changeType formData  string true "操作类型1:打包 2:同步 3更新"
// @param   path formData  string true "路径"
// @param   branchId formData  string true "版本编号"
// @router  /version/platformver-edit [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *versionMgrApi) PlatformverEdit(r *ghttp.Request) {
	var req model.PlatformVersionPath
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}
	u := service.Context.GetUser(r.Context())
	req.UserId = u.Id
	service.Version.PlatformVerPathEidt(&req)

	response.JsonSuc(r)
}

// @summary 更新操作工具数据
// @tags 设置
// @produce json
// @param   id formData  int false "id, 只有更新时传入"
// @param   name formData  int true "名称"
// @param   PlatformVersionId formData  string true "版本编号"
// @param   state formData  int true "启用状态"
// @param   shPath formData  string true "路径"
// @router  /version/vertoolchange-edit [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *versionMgrApi) VertoolchangeEdit(r *ghttp.Request) {
	var req model.VersionToolChange
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}
	u := service.Context.GetUser(r.Context())
	req.UserId = u.Id
	service.Version.VerToolChangeEdit(&req)

	response.JsonSuc(r)
}

type DelReq struct {
	Id int `json:"id"`
}

// @summary 删除路径数据
// @tags 设置
// @produce json
// @param   id formData  int true "id"
// @router  /version/del-branchpath [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *versionMgrApi) DelBranchpath(r *ghttp.Request) {
	var req DelReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}
	if req.Id < 1 {
		response.JsonFail(r)
	}

	_, err := dao.BranchPath.Delete(dao.BranchPath.Columns.Id, req.Id)
	if err != nil {
		defErr.Panic(define.DataOptFail, err.Error())
	}
	response.JsonSuc(r)
}

// @summary 删除平台路径数据
// @tags 设置
// @produce json
// @param   id formData  int true "id"
// @router  /version/del-platformverpath [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *versionMgrApi) DelPlatformverpath(r *ghttp.Request) {
	var req DelReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}
	if req.Id < 1 {
		response.JsonFail(r)
	}

	_, err := dao.PlatformVersionPath.Delete(dao.PlatformVersionPath.Columns.Id, req.Id)
	if err != nil {
		defErr.Panic(define.DataOptFail, err.Error())
	}
	response.JsonSuc(r)
}

// @summary 删除操作工具路径数据
// @tags 设置
// @produce json
// @param   id formData  int true "id"
// @router  /version/del-vertoolchange [POST]
// @success 200 {object} response.JsonResponse "执行结果"
func (a *versionMgrApi) DelVertoolchange(r *ghttp.Request) {
	var req DelReq
	if err := r.ParseForm(&req); err != nil {
		defErr.Panic(define.Fail, err.Error())
	}
	if req.Id < 1 {
		response.JsonFail(r)
	}

	_, err := dao.VersionToolChange.Delete(dao.VersionToolChange.Columns.Id, req.Id)
	if err != nil {
		defErr.Panic(define.DataOptFail, err.Error())
	}
	response.JsonSuc(r)
}
